/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.core.process;

import java.io.File;
import java.io.IOException;
 
public class ExecTest {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            // Configure
            String filePath = "test.data";
            File logDestination = new File("myLog.log");
            String path = "cmd /c start \"parser - running\" /MIN execute.bat \""+filePath + "\" \""+logDestination.getAbsolutePath()+"\"";
        
            System.out.println("> Starting process");
            Process process = Runtime.getRuntime().exec( path );
            
            System.out.println("> Start reading STDOUT and STDERR from the process");
            ProcessStreamReader inputStream = new ProcessStreamReader( process.getInputStream() );
            ProcessStreamReader errorStream = new ProcessStreamReader( process.getErrorStream() );
            inputStream.start();
            errorStream.start();
            
            System.out.println("> Waiting for the external process to complete.");
            process.waitFor();
            
            System.out.println("> Waiting for both reader threads to die");
            inputStream.join();    
            errorStream.join();  
            
            System.out.println("> Checking to see that there nothing was reported on STDERR");
            String errString = errorStream.getString();
            if(errString != null && errString.trim().length() > 0) {
               System.err.println("Problems occured while trying to access the external process: " + path);
               System.err.println( errString );
            }
            
            System.out.println("> Checking the STDOUT coming from the process.");
            String inString = inputStream.getString();
            System.out.println(inString);
            
            // Check for the existance of the log file
            if(logDestination.exists()) {
                System.out.println("> Log file exists at: " + logDestination.getAbsolutePath());
                // Check the length of the log file.
                System.out.println( "> Log file length: " + logDestination.length());
                
            } else {
                System.out.println("> Log file wasn't created.");
            }
            
        } catch(IOException ioex) {
            ioex.printStackTrace();
        } catch(InterruptedException iex) {
            iex.printStackTrace();
        }
    }
}
